<?php

namespace app\common\model;

use think\Model;

class Wx_Pay_Record extends Model
{

    protected $table = 'wx_pay_record';

    /**
     * 根据外部订单号查询
     * @param int $outSn
     */
    public function getOneByOutSn($outSn)
    {
        return $this->where('pay_out_trade_num', $outSn)->find();
    }

    /**
     * 根据商户订单号查询
     * @param string $orderSn
     */
    public function getRecordByOrderSn($orderSn)
    {
        return $this->order('id', 'desc')->where('pay_order_sn' ,$orderSn)->find();
    }

    /**
     * 保存支付记录
     * @param array $data
     */
    public function saveRecord($data)
    {
        $rel = $this->getRecordByOrderSn($data['pay_order_sn']);
        if ($rel) {
            // if($rel['pay_paid']){
            // 	return error('订单已存在');
            // }
            $this->where('id', $rel['id'])->update($data);
        }
        return $this->save($data);
    }
}